package util;

import java.util.ArrayList;

/**
 * TimePair holds two lists of start times and finish times respectively. It is
 * used to hold the times needed for either a lap race or a stage race. The
 * class uses lists of times to be able to keep track of multiple start and
 * finish times. It also provides methods for fetching the total time of this
 * particular pair.
 * 
 * @author team07
 * 
 */
public class TimePair {
	private ArrayList<Long> startTime;
	private ArrayList<Long> finishTime;
	private int stageNumber = 0;

	/**
	 * Creates a new empty TimePair
	 */
	public TimePair() {
		startTime = new ArrayList<Long>();
		finishTime = new ArrayList<Long>();

	}

	/**
	 * Returns a ArrayList<Long> of the starttimes
	 * 
	 * @return The starttimes
	 */
	public ArrayList<Long> getStart() {
		return startTime;
	}

	/**
	 * Returns a ArrayList<Long> of the finishtimes
	 * 
	 * @return The finishtimes
	 */
	public ArrayList<Long> getFinish() {
		return finishTime;
	}

	/**
	 * Adds the specified starttime to the starttimes
	 * 
	 * @param start
	 *            The specified starttime
	 */
	public void addStart(long start) {
		startTime.add(start);
	}

	/**
	 * Adds the specified finishtime to the finishtimes
	 * 
	 * @param finish
	 *            The specified finishtime
	 */
	public void addFinish(long finish) {
		finishTime.add(finish);
	}

	/**
	 * Returns the time between start and finnish this TimePair holds.
	 * 
	 * @return The total time.
	 */

	public long getTotalTime() {
		return finishTime.get(0) - startTime.get(0);
	}

	/**
	 * Returns a string of the starttime
	 * 
	 * @return The starttime string
	 */
	public String getStartTimeString() {
		if (startTime.size() == 0) {
			return "Start?";
		}
		return TimeConversion.millisToHHMMss(startTime.get(0));
	}

	/**
	 * Returns a string of the finishtimes calculated with the number of laps
	 * specified
	 * 
	 * @param nbrLaps
	 *            The number of laps
	 * @return String containing the contestants' finishtimes
	 */
	public String getFinishTimeString(int nbrLaps) {
		if (finishTime.size() == 0) {
			return "Slut?";
			//return "";
		}
		try {
			return TimeConversion.millisToHHMMss(finishTime.get(nbrLaps - 1));
		} catch (IndexOutOfBoundsException e) {
			return "";
		}
	}

	/**
	 * Sets the stage number of this TimePair to the specified number.
	 * 
	 * @param i
	 *            The number of the stage
	 */
	public void setStageNumber(int i) {
		this.stageNumber = i;
	}

	/**
	 * Get the stagenumber for this timepair
	 * @return int, stagenumber
	 */
	public int getStageNumber(){
		return stageNumber;
	}
	
	/**
	 * Checks if the Object o is a TimePair and if that TimePair 
	 * has the number as this one.
	 * 
	 * @param other
	 *            An Object that is lates typecasted to TimePair
	 * @return True or false
	 */
	public boolean equals(Object other) {
		try {
			return (((TimePair) other).stageNumber == this.stageNumber);
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * Override the hashCode implementation
	 */
	@Override
	public int hashCode(){
		return this.stageNumber;
	}
}